package bitwiseOperation;

//https://leetcode.cn/problems/number-of-1-bits/description/
public class l_191 {
    public int hammingWeight(int n) {
        int ans = 0;
        while(n != 0)
        {
            //n & (n - 1) 就是让n二进制位最右侧的1变为0
            //(n - 1) 就是将n二进制位最右侧的1, 右边的区域包含1全部变成相反
            n = n & (n - 1);
            ans++;
        }
        return ans;
    }
}
